package org.cttic.visualization.visualization;

import org.cttic.visualization.visualization.topology.GraphData;
import org.cttic.visualization.visualization.topology.service.GraphDistanceDijkstra;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.net.InetAddress;

@SpringBootApplication
public class VisualizationApplication implements CommandLineRunner {


//    @Bean
//    TransportClient getTransportClient(){
//        Settings esSetting = Settings.builder()
//                .put("cluster.name", "es-111") //集群名字
//                .put("client.transport.sniff", true)//增加嗅探机制，找到ES集群
//                .put("thread_pool.search.size", Integer.parseInt("5"))//增加线程池个数，暂时设为5
//                .build();
//        PreBuiltTransportClient transportClient = new PreBuiltTransportClient(esSetting);
//        new TransportAddress()
//        TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port));
//        transportClient.addTransportAddresses(transportAddress);
//
//        return new PreBuiltTransportClient(esSetting);
//    }

    public static void main(String[] args) {
        //        System.out.println("===========================================");
        /**
         * Springboot整合Elasticsearch 在项目启动前设置一下的属性，防止报错
         * 解决netty冲突后初始化client时还会抛出异常
         * java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]
         */
        System.setProperty("es.set.netty.runtime.available.processors", "false");

        SpringApplication.run(VisualizationApplication.class, args);
    }

    @Autowired
    GraphData graphData;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("================开始加载拓扑关系。。。");
        graphData.initGraph();
    }
}
